#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>

#include "Common.h"
#include "url.h"


static unsigned char hexchars[] = "0123456789ABCDEF";

static int url_htoi(char *s)
{
	int value;
	int c;

	c = ((unsigned char *)s)[0];
	if (isupper(c))
		c = tolower(c);
	value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;

	c = ((unsigned char *)s)[1];
	if (isupper(c))
		c = tolower(c);
	value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;

	return (value);
}

char *  url_encode(char const *s, int len, int * new_length)
{
	register unsigned char c;
	unsigned char *to, *start;
	unsigned char const *from, *end;
	
	from = (unsigned char *)s;
	end = (unsigned char *)s + len;
	start = to = (unsigned char *) malloc(len * 3 + 1);

	while (from < end) {
		c = *from++;

		if (c == ' ') {
			*to++ = '+';
		} else if ((c < '0' && c != '-' && c != '.') ||
				   (c < 'A' && c > '9') ||
				   (c > 'Z' && c < 'a' && c != '_') ||
				   (c > 'z')) {
			to[0] = '%';
			to[1] = hexchars[c >> 4];
			to[2] = hexchars[c & 15];
			to += 3;
		} else {
			*to++ = c;
		}
	}
	*to = 0;
	if (new_length) {
		*new_length = to - start;
	}
	return (char *) start;
}



String  string_url_encode(const String & str)
{
	register unsigned char c;
	unsigned char const *from, *end;
	String ret;
	
	from = (unsigned char *)str.c_str();
	end = (unsigned char *)from + str.length();
	

	while (from < end) {
		c = *from++;

		if (c == ' ') {
			ret.push_back('+');
		} else if ((c < '0' && c != '-' && c != '.') ||
				   (c < 'A' && c > '9') ||
				   (c > 'Z' && c < 'a' && c != '_') ||
				   (c > 'z')) {
			ret.push_back('%');
			ret.push_back(hexchars[c >> 4]);
			ret.push_back(hexchars[c & 15]);
		} else {
			ret.push_back(c);
		}
	}
	
	return ret;
}


char * url_decode(char *str, int len)
{
	char *dest = str;
	char *data = str;

	while (len--) {
		if (*data == '+') {
			*dest = ' ';
		}
		else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1)) 
				 && isxdigit((int) *(data + 2))) {
			*dest = (char) url_htoi(data + 1);
			data += 2;
			len -= 2;
		} else {
			*dest = *data;
		}
		data++;
		dest++;
	}
	*dest = '\0';
	return str;
}


String  string_url_decode(const String & str)
{
	char *data = (char *)str.c_str();
	size_t len =str.length();
	String ret;
	
	while (len--) {
		if (*data == '+') {
			ret.push_back(' ');
		}
		else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1)) 
				 && isxdigit((int) *(data + 2))) {
			ret.push_back((char) url_htoi(data + 1));
			data += 2;
			len -= 2;
		} else {
			ret.push_back(*data);
		}
		data++;
	}
	return ret;
}